package org.elzat.healthboxgatewayapp.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elzat.healthboxgatewayapp.util.ResultEntity;
import org.elzat.healthboxgatewayapp.credentials.HealthBoxUserRole;
import org.elzat.healthboxgatewayapp.dto.HealthBoxUserVO;
import org.elzat.healthboxgatewayapp.service.HealthBoxUserQueryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "health-box-api/v1/users",
        produces = "application/json; charset=utf-8")
@AllArgsConstructor
@Slf4j
public class UserQueryController {

    private final HealthBoxUserQueryService queryService;

    /**
     * 开放给管理员的对用户进行query搜索的接口
     * 接口包含三个参数分别为userPhone、idNumber、role, 分别对应手机号、身份证号和用户角色
     * 三个参数都是可选参数:
     *  1. 若都为空即, 请求url为GET .../users, 相当于请求获取所有用户信息
     *  2. 选择其中两个参数, 即请求满足该两个参数的用户信息, 以此类推
     *
     * 返回结果为data为HealthBoxUserVO类型的ResultEntity类对象, 即返回的data要么为null, 要么是个列表:
     *  {
     *      code: [成功为200, 失败为400],
     *      msg: [对应的反馈消息],
     *      data: 失败时为null, 成功时
     *          [
     *              {
     *                  "id": [用户id],
     *                  "userPhone": [用户手机号],
     *                  "locked": [用户是否已被封禁],
     *                  "name": [用户名],
     *                  "idNumber": [用户身份证号],
     *                  "role": [用户角色]
     *              },
     *              {
     *                  [上述信息]
     *              },
     *              ...
     *          ]
     *  }
     * */
    @GetMapping
    public ResultEntity<List<HealthBoxUserVO>> getAllUsers(
            @RequestParam(name = "userPhone", required = false, defaultValue = "")
            String userPhone,
            @RequestParam(name = "idNumber", required = false, defaultValue = "")
            String idNumber,
            @RequestParam(name = "role", required = false, defaultValue = "")
            String role
    ) {

        log.info("Got request to GET users with query: userPhone=" + userPhone
                + "&idNumber=" + idNumber + "&role=" + role);

            // GET .../users
        if ("".equals(userPhone) && "".equals(idNumber) && "".equals(role)) {
            List<HealthBoxUserVO> all = queryService.getAllUsers();
            return ResultEntity
                    .ok()
                    .msg("返回所有用户信息")
                    .data(all);

            // GET .../users?userPhone=[user phone number]
        } else if ("".equals(idNumber) && "".equals(role)) {
            Optional<HealthBoxUserVO> maybe = queryService.getUserByUserPhone(userPhone);
            return maybe.isPresent() ?
                    ResultEntity.ok().msg("返回手机号为" + userPhone + "的用户信息")
                            .data(Collections.singletonList(maybe.get()))
                    : ResultEntity.failure().msg("不存在对应用户").build();

            // GET .../users?idNumber=[user id number]
        } else if ("".equals(userPhone) && "".equals(role)) {
            Optional<HealthBoxUserVO> maybe = queryService.getUserByIdNumber(idNumber);
            return maybe.isPresent() ?
                    ResultEntity.ok().msg("返回身份证号为" + idNumber + "的用户信息")
                            .data(Collections.singletonList(maybe.get()))
                    : ResultEntity.failure().msg("不存在对应用户").build();

            // GET .../users?role=[user role]
        } else if ("".equals(userPhone) && "".equals(idNumber)) {
            HealthBoxUserRole userRole;
            try {
                userRole = HealthBoxUserRole.valueOf(role);
            } catch (IllegalArgumentException e) {
                return ResultEntity.failure().msg("不存在该用户角色").build();
            }
            List<HealthBoxUserVO> all = queryService.getAllByRole(userRole);
            return ResultEntity
                    .ok()
                    .msg("返回所有用户角色为" + role + "的用户")
                    .data(all);

            // GET .../users?userPhone=[user phone number]&idNumber=[user id number]
        } else if ("".equals(role)) {
            Optional<HealthBoxUserVO> maybe = queryService
                    .getUserByUserPhoneAndIdNumber(userPhone, idNumber);
            return maybe.isPresent() ?
                    ResultEntity.ok()
                            .msg("返回手机号为" + userPhone + "且身份证号为" + idNumber + "的用户")
                            .data(Collections.singletonList(maybe.get()))
                    : ResultEntity.failure().msg("不存在对应用户").build();

            // GET .../users/?userPhone=[user phone number]&role=[user role]
        } else if ("".equals(idNumber)) {
            Optional<HealthBoxUserVO> maybe = queryService
                    .getUserByUserPhoneAndRole(userPhone, role);
            return maybe.isPresent() ?
                    ResultEntity.ok()
                            .msg("返回手机号为" + userPhone + "且用户角色为" + role + "的用户")
                            .data(Collections.singletonList(maybe.get()))
                    : ResultEntity.failure().msg("不存在对应用户").build();

            // GET .../users/?idNumber=[user id number]&role=[user role]
        } else if ("".equals(userPhone)) {
            Optional<HealthBoxUserVO> maybe = queryService
                    .getUserByIdNumberAndRole(idNumber, role);
            return maybe.isPresent() ?
                    ResultEntity.ok()
                            .msg("返回身份证号为" + idNumber + "且用户角色为" + role + "的用户")
                            .data(Collections.singletonList(maybe.get()))
                    : ResultEntity.failure().msg("不存在对应用户").build();

            // GET .../users/?userPhone=[user phone number]&idNumber=[user id number]&role=[user role]
        } else {
            Optional<HealthBoxUserVO> maybe = queryService.getUserByUserPhone(userPhone);
            if (!maybe.isPresent())
                return ResultEntity
                        .failure()
                        .msg("不存在对应用户")
                        .build();
            HealthBoxUserVO vo = maybe.get();
            if (idNumber.equals(vo.getIdNumber()) && role.equals(vo.getRole().name()))
                return ResultEntity
                        .ok()
                        .msg("返回手机号为" + userPhone + "且身份证号为" +
                                idNumber + "且用户角色为" + role + "的用户")
                        .data(Collections.singletonList(vo));
            return ResultEntity
                    .failure()
                    .msg("不存在对应用户")
                    .build();
        }
    }



}
